<?php

namespace common\models\tsline;

use Yii;
use common\models\member\Member;

/**
 * This is the model class for table "{{%ts_article_comment}}".
 *
 * @property int $id
 * @property int $article_id 文章ID
 * @property int $comment_id 评论ID
 * @property int $user_id 用户ID
 * @property string $content 评论内容
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class TslineArticleComment extends \yii\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\TimestampBehavior;

    const STATUS_ACTIVE = 1;
    const STATUS_DELETED = -1;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%ts_article_comment}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['article_id', 'comment_id', 'device_id', 'user_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['content'], 'required'],
            [['content'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'article_id' => '文章ID',
            'comment_id' => '评论ID',
            'user_id' => '用户ID',
            'device_id' => '设备ID',
            'content' => '评论内容',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    public function getChildren() {
        return $this->hasMany(TsArticleComment::className(), ['comment_id' => 'id']);
    }

    public function getParent() {
        return $this->hasOne(TsArticleComment::className(), ['id' => 'comment_id']);
    }

    public function getUser() {
        return $this->hasOne(Member::className(), ['uid' => 'user_id']);
    }

    public function getLikes() {
        return $this->hasMany(TsUserLike::className(), ['model_id' => 'id'])->where(['model' => 'article_comment']);
    }

    public function getLikesnum() {
        return count($this->likes);
    }

}
